import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'package:namewava/utils/jc_cache.dart';
import 'package:namewava/utils/js_request.dart';

// ignore: must_be_immutable
class JcGlobal extends InheritedWidget {
  JcGlobal({
    super.key,
    required this.baseUrl,
    required this.baseLogUrl,
    required this.waveConfig,
    required this.waveStrategy,
    required this.waveValidToken,
    required this.waveUserInfo,
    required this.wavePolicy,
    required this.waveSaveUserInfo,
    required this.waveUpdateAvatar,
    required this.waveGoods,
    required this.waveCreateRecharge,
    required this.waveRechargeCheck,
    required this.waveCoinsSpend,
    required this.waveLogout,
    required this.waveDeleteAccount,
    required this.waveLogin,
    required this.waveLogUp,
    this.logIndex = 0,
    required super.child,
  });

  ///获取相关配置
  final String waveConfig;

  ///登录
  final String waveLogin;

  ///获取策略信息
  final String waveStrategy;

  ///Token 是否有效
  final String waveValidToken;

  ///获取用户信息
  final String waveUserInfo;

  ///获取图片上传相关参数
  final String wavePolicy;

  ///保存用户信息
  final String waveSaveUserInfo;

  ///更新用户头像
  final String waveUpdateAvatar;

  ///内购商品列表
  final String waveGoods;

  ///创建订单
  final String waveCreateRecharge;

  ///验单
  final String waveRechargeCheck;

  ///消费
  final String waveCoinsSpend;

  ///退出
  final String waveLogout;

  ///删除
  final String waveDeleteAccount;

  ///日志上报
  final String waveLogUp;

  ///接口浅醉
  final String baseUrl;

  ///日志上报接口浅醉
  final String baseLogUrl;

  int logIndex;

  static JcGlobal of(BuildContext context) {
    final JcGlobal? result =
        context.dependOnInheritedWidgetOfExactType<JcGlobal>();
    assert(result != null, 'No GlobalState found in context');
    return result!;
  }

  @override
  bool updateShouldNotify(covariant JcGlobal oldWidget) {
    return true;
  }

  logReport(Map parms) {
    DateTime now = DateTime.now();
    int timestamp = now.millisecondsSinceEpoch;

    var userId = JcCache.getInstance().get('wavaUserId');
    userId ??= '';

    var appInfo = JcCache.getInstance().get('appinfo');

    ///{ver: 1.0.0, p_ver: 1, pkg: test.bigegg.ios, sys_lan: zh-Hans-CN, lang: zh-Hans-CN, model: sdk_gphone16k_x86_64, device-id: AP31.240617.003, platform: Android}
    Map info = jsonDecode(appInfo);
    String lanId = strToMd5('$timestamp${info['device-id']}');
    parms['duration'] = 0;
    parms['event'] = 'pv';
    parms['tm'] = timestamp;
    //print(parms);

    Map<String, dynamic> lastP = {
      'data': [parms],
      'lan_id': lanId,
      'log_type': 'event',
      'device-id': info['device-id'],
      'user_id': userId,
      'pkg': info['pkg'],
      'chn_id': '',
      'subtype': 'global_behavior',
      'ver': info['ver'],
      'platform': info['platform'],
      'model': info['model'],
      'p_ver': info['p_ver'],
      'af_adset_id': '',
      'af_status': '',
      'sys_lan': info['sys_lan'],
      'sec_id': logIndex++,
      'behavior': 'event',
      'country': '',
      'campaign': '',
      'campaign_id': '',
      'af_adset': '',
      'af_agency': '',
      'af_channel': '',
      'utm_source': '',
      'af_adgroup_id': '',
    };
    print(lastP);
    JsRequest.wavaLogRequest(p: lastP);
  }

  String strToMd5(String str) {
    final strEncoder = const Utf8Encoder().convert(str);
    final result = md5.convert(strEncoder);

    return result.toString();
  }
}
